package com.itheima.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

@WebServlet("/bServlet")
public class BServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BServlet doGet....");
        //  // name=zhangsan; email="zs@163.com"; Idea-ad9301ab=70cf216e-c90b-489b-a336-559fba3dd5dd
        String cookieString = req.getHeader("Cookie");
         // 读取Cookie
        Cookie[] cookies = req.getCookies();
        // 遍历
        for (Cookie cookie:cookies){
            System.out.println(cookie.getName()+" "+cookie.getValue());
            // 比较的,你需要确定哪个Cookie存的中文
            if (cookie.getName().equals("addr")){
                // 把UTF-8的解码
                String decodeNameValue = URLDecoder.decode(cookie.getValue(), "utf-8");
                System.out.println(cookie.getName()+" "+decodeNameValue);
            }
        }
    }
}
